package cn.tedu.javagame.Plane;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class Hero {
    //属性
    int x,y;
    int life,magic,score,gold;
    int fire = 1;//1代表单倍火力  2代表双倍火力
    Image heroImg;
    Image[] heroImgs = new Image[10];
    //构造
    public Hero(){
        this.x = 200;
        this.y = 500;
        this.life = 100;
        this.magic = 100;
        this.score = 0;
        this.gold = 0;
        this.heroImg = new ImageIcon("plane/ws00.png").getImage();
        for(int i=0;i<heroImgs.length;i++){
            heroImgs[i] = new ImageIcon("plane/ws0"+i+".png").getImage();
        }
    }
    //方法
    public void move(int x,int y){
        this.x = x;
        this.y = y;
    }
    //实现英雄机的动态效果,此方法需要不停的被调用，
    //方法内部每一次调用的时候从一个容器中依次取出一个图片，赋值给heroImg属性。
    int index=0;
    public void step(){
        index++;
        if(index==Integer.MAX_VALUE){index=0;}
        int i =index/15%heroImgs.length;
//        System.out.println(i);
        heroImg = heroImgs[index/15%heroImgs.length];
    }

    //发射子弹
    public List<Bullet> shoot(){
        /***首先创建一个集合
         * 根据火力值，创建一个子弹或多个子弹，添加到集合中
         * 最后返回这个集合
         */
        List<Bullet> bullets = new ArrayList<>();
        if(fire==1){
            //找到英雄机的 嘴巴 的位置坐标
            int x = this.x + this.heroImg.getWidth(null)/2;
            int y = this.y;
            Bullet bullet = new Bullet(x,y);
            bullets.add(bullet);
        }
        if(fire==2){
            //找到英雄机的左上角和右上角的坐标
            int x1 = this.x;
            int y1 = this.y;
            int x2 = this.x + this.heroImg.getWidth(null);
            int y2 = this.y;
            Bullet bullet1 = new Bullet(x1,y1);
            Bullet bullet2 = new Bullet(x2,y2);
            bullets.add(bullet1);
            bullets.add(bullet2);
        }
        return bullets;
    }
    //碰到了敌机，生命值减少
    public boolean hitEnemy(Enemy enemy) {
        // 获取英雄和敌人的边界
        int heroLeft = this.x;
        int heroRight = this.x + this.heroImg.getWidth(null);
        int heroTop = this.y;
        int heroBottom = this.y + this.heroImg.getHeight(null);

        int enemyLeft = enemy.x;
        int enemyRight = enemy.x + enemy.img.getWidth(null);
        int enemyTop = enemy.y;
        int enemyBottom = enemy.y + enemy.img.getHeight(null);

        // 检测两个矩形是否重叠
        boolean isOverlap = heroLeft < enemyRight &&
                heroRight > enemyLeft &&
                heroTop < enemyBottom &&
                heroBottom > enemyTop;

        if (isOverlap) {
            System.out.println("hp-10");
            this.life -= 10;
            return true;
        }

        return false;
    }
    //碰撞了小红心，就增加奖励
    public boolean hitHeart(Heart heart){
        // 获取英雄和生命值道具的边界
        int heroLeft = this.x;
        int heroRight = this.x + this.heroImg.getWidth(null);
        int heroTop = this.y;
        int heroBottom = this.y + this.heroImg.getHeight(null);

        int heartLeft = heart.x;
        int heartRight = heart.x + heart.img.getWidth(null);
        int heartTop = heart.y;
        int heartBottom = heart.y + heart.img.getHeight(null);

        // 检测两个矩形是否重叠
        boolean isOverlap = heroLeft < heartRight &&
                heroRight > heartLeft &&
                heroTop < heartBottom &&
                heroBottom > heartTop;

        if (isOverlap) {
            // 处理生命值增加逻辑
            this.life += 10; // 假设Heart类有getLifeValue方法
            System.out.println("hp+10");
            return true;
        }
        return false;

    }
}
